Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
A LinkExecute event occurs when a source form receives a command string from the destination in the DDE conversation. The LinkExecute event procedure format is:
    Sub Form_Execute (Cmd As String, Cancel As Integer)
    . . .
    End Sub
The Cmd argument contains the command string that was sent by the DDE destination. No defined DDE command language is shared among applications, and the specific commands and syntax that a particular DDE source understands differ from program to program. To enable your Visual Basic program to respond to such commands, you have to write the code. The Cancel argument passed to the Execute event procedure determines the response sent back to the DDE destination application—the one that sent the command string. If Cancel is set to 0, the Visual Basic program sends a positive acknowledgment, usually meaning that the command string was received and acted upon. If Cancel is set to any nonzero value, a negative acknowledgment occurs. A negative acknowledgment is automatically sent when the program has no LinkExecute event procedure. #### Pasting Links Into A Visual Basic Program One way to create a link in a Visual Basic program is to paste it using the Clipboard. If you have worked with Windows applications, such as a word processor, you have probably seen the Paste Link command—usually found on the Edit menu. Using this command creates a DDE link with any other DDE-aware Windows application. The general procedure for establishing links using the Clipboard is shown here: 1. Make the source application active and select the data to be linked (for example, a column of cells in a spreadsheet). 2. Still in the source application, select Copy (this command is almost always found on the Edit menu). The Copy command places the data and associated link information on the Clipboard. 3. Make the destination application active. 4. Move to the location where you want to place the linked data. 5. Select Paste Link. An automatic link is established between the two applications. In this section, I’ll include a Paste Link command in the sample Visual Basic programs. This type of DDE support can greatly increase a program’s usefulness by permitting users to cut and paste DDE links quickly and easily. #### DDE And The Clipboard To use the Copy...Paste Link command sequence to create a DDE link, the Windows Clipboard is used as the intermediate storage location where the link information is kept. A Visual Basic program accesses the Windows Clipboard by means of the Clipboard object. The Clipboard is not simply a storage buffer that can hold and later return whatever data it receives. No, the Clipboard is actually pretty clever: It has the ability to hold data in several different formats. In fact, the Clipboard can hold more than one data item if they are in different formats. Furthermore, the Clipboard can tell you whether it currently contains a data item of a specified format. The data formats that the Clipboard can work with are: • Text • Bitmap (BMP files) • Metafile (WMF files) • Device-independent bitmap (DIB files) • Color palette • DDE link information The meaning of the first four items should be clear: Text is, well, text, and the next three are different formats used to hold graphics images. A color palette is a set of color values that is used for the pixels in a specific graphic. But what is meant by DDE link information? This is the information required to establish a DDE link to the data on the Clipboard. Let’s see how this works. When you use the Copy command, Windows applications that support active DDE links not only place a copy of the selected data (text, a bitmap, or whatever) on the Clipboard, they also place the information—the application, topic, and item information—that is needed to establish a DDE link to the original data. If you switch to another application and execute the Paste command (not the Paste Link command), only the data will be copied from the Clipboard into the destination, and no link will be established. By executing the Paste Link command, however, you will obtain both the data and the DDE link information from the Clipboard, and an active DDE link will be created. How can a Visual Basic application determine if data in a particular format is available on the Clipboard? The Clipboard object’s GetFormat method is designed for just this task. The method’s syntax is:
    result = Clipboard.Getformat(type)
The type argument specifies the data format, and you can specify type with the Windows global constants, shown in Table 9.1. The GetFormat method returns True if the specified type of data is present on the Clipboard, False if not. For example, you could test for the presence of text data as follows:
    If Clipboard.GetFormat(vbCFText) Then
        . . .
        ‘ Text data present on clipboard.
    Else
        . . .
        ‘ Text data not present.
    End If
The ability to determine whether a specific data format is present on the Clipboard can be useful in a variety of situations. A graphics program, for example, could enable a Paste Picture command only if data in one of the three graphics formats is present on the Clipboard. For DDE, however, it’s the DDE link information that is important. You need this information for the Paste Link command to work properly. Let’s see how to obtain it. To retrieve the DDE link information, use the Clipboard object’s GetText method. You can use this method to retrieve text data from the Clipboard; but if you pass the argument vbCFLink (value = &HBF00), GetText returns DDE link information (or an empty string if no link information is on the Clipboard):
    LinkInfo = Clipboard.GetText(vbCFLink)
The DDE link information is returned in a specific format. The application name comes first, followed by a vertical pipe character (CHR$(124) ), then the topic. The topic is followed by an exclamation point; the item is at the end:
    application|topic!item
Table 9.1 Windows global constants related to DDE.
Constant | Value | Format
--- | --- | ---
vbCFLink | HBF00 | DDE conversation information
vbCFText | 1 | Text
vbCFBitmap | 2